package com.yuexia.yxim.client.handler;

import com.yuexia.yxim.common.bean.msg.ProtoMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * ChatMsgHandler
 *
 * @author wjp
 * @date 2020/6/2
 */
@Service
@Slf4j
@ChannelHandler.Sharable
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        //判断消息实例
        if (msg == null || !(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        //判断消息类型
        ProtoMsg.Message pkg = (ProtoMsg.Message) msg;
        ProtoMsg.HeadType pkgType = pkg.getType();

        if (!pkgType.equals(ProtoMsg.HeadType.MESSAGE_RESPONSE)) {
            super.channelRead(ctx, msg);
            return;
        }

        ProtoMsg.MessageRequest messageRequest = pkg.getMessageRequest();
        String content = messageRequest.getContent();
        String uid = messageRequest.getFrom();

        log.info("接收到消息 uid = {},内容 = {}", uid, content);

    }
}
